package com.hy.formula.function;

import java.math.BigDecimal;

import com.hy.formula.common.BigDecimalUtils;
import com.hy.formula.exception.FormulaException;
import com.hy.formula.runtime.FRuntimeContext;

public class DivFunc extends AbsNumCalculateFunc implements IFunction {

	@Override
	public String getName() {
		return "div";
	}

	@Override
	public Object execute(FRuntimeContext context, Object... params) {
		if(params == null || params.length != 2) {
			throw new FormulaException("参数个数不对");
		}
		Object o1 = params[0];
		Object o2 = params[1];
		if(hasBigDecimal(o1, o2)) {
			
			BigDecimal num1 = new BigDecimal(o1.toString());
			BigDecimal num2 = new BigDecimal(o2.toString());
			return BigDecimalUtils.div(num1,num2);
			
		}else if(hasDouble(o1,o2)){
			String s1 = params[0].toString();
			Double num1 = Double.valueOf(s1);
			
			String s2 = params[1].toString();
			Double num2 = Double.valueOf(s2);
			
			return num1 / num2;
		}else if(hasFloat(o1,o2)){
			String s1 = params[0].toString();
			float num1 = Float.valueOf(s1);
			
			String s2 = params[1].toString();
			float num2 = Float.valueOf(s2);
			
			return num1 / num2;
		}else{
			return (int)params[0] * 0.1 / (int)params[1];
		}
	}

	@Override
	public String description() {
		return "除法,div(1,2)等价于1/2=0.5";
	}

}
